import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest, HttpResponse } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { filter, map } from 'rxjs/operators';
import { MineService } from './mine.service';

@Injectable()
export class HttpInterceptorService implements HttpInterceptor {

  constructor(private mineService: MineService) { }

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    let ret: Observable<HttpEvent<any>>;
    const accessToken = this.mineService.getAccessToken();
    if (accessToken) {
      const clonedRequest = req.clone({ setHeaders: { 'Access-Token': accessToken } });
      ret = next.handle(clonedRequest);
    } else {
      ret = next.handle(req);
    }
    return ret;
  }
}
